import { NextFunction, Request, Response } from "express";
import Base from "../../common/controller/base";
import { checkField, getUserInfo } from "../../utils/common";
import paperSer from "./service";

class Paper extends Base {
  /**
   * 创建答卷
   * @param req
   * @param res
   * @param next
   * @returns
   */
  createPaper = async (req: Request, res: Response, next: NextFunction) => {
    const { name, time, classId, subjectIds } = req.body;

    // 校验参数
    if (!checkField(name, subjectIds, time))
      return next({ code: 400, msg: "缺少参数" });

    // 获取用户的id
    const { id } = getUserInfo(req);

    try {
      // 创建答卷并获取对应的试卷id
      const paperId = await paperSer.createPaper(name, id, classId, time);

      // 插入题目
      await Promise.all(
        subjectIds.map(async (id: string) => {
          await paperSer.addSubject(paperId, id);
        })
      );

      this.success(res, { msg: "创建成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取教师创建的所有的答卷
   * @param req
   * @param res
   * @param next
   */
  getAll = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = getUserInfo(req);
    try {
      const data = await paperSer.getAll(id);
      this.success(res, { msg: "获取教师的答卷成功", data: data.reverse() });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取一张答卷
   * @param req
   * @param res
   * @param next
   */
  getOnePaper = async (req: Request, res: Response, next: NextFunction) => {
    const { paperId } = req.params;
    // 校验参数
    if (!checkField(paperId)) return next({ code: 400, msg: "缺少参数" });
    try {
      const data = await paperSer.getOnePaper(paperId);
      this.success(res, { msg: "获取答卷试题成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取所有的试题
   * @param req
   * @param res
   * @param next
   */
  getAllSubjects = async (_req: Request, res: Response, next: NextFunction) => {
    try {
      const data = await paperSer.getAllSubjects();
      this.success(res, { msg: "获取全部试题成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 删除试卷
   * @param req
   * @param res
   * @param next
   */
  delPaper = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = req.params;
    try {
      await paperSer.delPaper(id);
      this.success(res, { msg: "删除试卷成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 添加题目
   * @param req
   * @param res
   * @param next
   */
  addSubject = async (req: Request, res: Response, next: NextFunction) => {
    const { options, name, score, ans, analysis = "无" } = req.body;
    // 检查字段
    if (!checkField(options, name, score, ans))
      return next({ code: 400, msg: "缺少参数" });
    try {
      // 添加选项
      const optionsId = await paperSer.addOptions(
        options.A,
        options.B,
        options.C,
        options.D
      );
      // 添加题目
      await paperSer.addSubjects(optionsId, ans, score, name, analysis);
      this.success(res, { msg: "添加题目成功", data: "" });
    } catch (e) {
      console.log(e);
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 删除试题
   * @param req
   * @param res
   * @param next
   */
  delSubject = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = req.params;
    try {
      paperSer.delSubject(id);
      this.success(res, { msg: "删除题目成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 添加答题
   * @param req
   * @param res
   * @param next
   */
  addAnswer = async (req: Request, res: Response, next: NextFunction) => {
    const { paperId, time, score, subjects } = req.body;

    if (!checkField(paperId, time, score, subjects))
      return next({ code: 400, msg: "缺少参数" });

    const { id } = getUserInfo(req);
    try {
      const ansId = await paperSer.addAnswer(id, time, paperId, score);
      subjects.forEach(async (s: any) => {
        await paperSer.addAnswerSubject(s.id, s.ans, s.uAns, ansId, id);
        if (s.corr) {
          await paperSer.incCorr(s.id);
        } else {
          await paperSer.incErr(s.id);
        }
      });
      this.success(res, { msg: "评卷成功", data: "" });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取答题情况
   * @param req
   * @param res
   * @param next
   */
  getAnswer = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = getUserInfo(req);

    try {
      const data = await paperSer.getAnswer(id);
      this.success(res, { msg: "获取成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取答卷详情
   * @param req
   * @param res
   * @param next
   */
  getAnswerDetail = async (req: Request, res: Response, next: NextFunction) => {
    const { id } = req.params;

    try {
      const data = await paperSer.getAnswerDetail(id);
      this.success(res, { msg: "获取详情", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取题目记录
   * @param req
   * @param res
   * @param next
   */
  getSubjectRecord = async (
    req: Request,
    res: Response,
    next: NextFunction
  ) => {
    const { id } = getUserInfo(req);
    try {
      const data = await paperSer.getSubjectRecord(id);
      this.success(res, { msg: "获取题目记录成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 搜题
   * @param req
   * @param res
   * @param next
   */
  search = async (req: Request, res: Response, next: NextFunction) => {
    const { w } = req.params;
    try {
      const data = await paperSer.searchSubject(w);
      this.success(res, { msg: "搜索题目成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取学生的答卷
   * @param req
   * @param res
   * @param next
   */
  getStuPapers = async (_req: Request, res: Response, next: NextFunction) => {
    try {
      const { id } = getUserInfo(_req);
      const data = await paperSer.getStuPapers(id);
      this.success(res, { msg: "获取学生的答卷成功", data: data.reverse() });
    } catch (e) {
      console.log(e);
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取练习题
   * @param _req
   * @param res
   * @param next
   */
  getExec = async (req: Request, res: Response, next: NextFunction) => {
    const { count } = req.params;
    try {
      const data = await paperSer.getExec(+count);
      this.success(res, { msg: "获取学生的答卷成功", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取教师试卷考试详情
   * @param req
   * @param res
   * @param next
   */
  getTchDataAna = async (req: Request, res: Response, next: NextFunction) => {
    const { paperId } = req.params;
    try {
      const data = await paperSer.getTchDataAna(paperId);
      this.success(res, { msg: "success", data });
    } catch (e) {
      next({ code: 500, msg: "服务器错误" });
    }
  };

  /**
   * 获取所有的试卷名字
   * @param req 
   * @param res 
   * @param next 
   */
  getAllPaperNames = async (req: Request, res: Response, next: NextFunction) => { 
    const {id} = getUserInfo(req)
    try {
      const data = await paperSer.getAllPaperName(id);
      this.success(res, { msg: "success", data });
    } catch(e) {
      next({ code: 500, msg: "服务器错误" });
    }
  }
}

export default new Paper();
